<template>
	<view :data-theme="theme" class="container">
		<view class="header">

			<view class="bg_color" style="padding: 24rpx 24rpx;">
				<u-search v-model="keyWord" @custom="searchProduct" @search="searchProduct" placeholder="请输入优惠券名称"
					actionText="搜索" :actionStyle="{color:'#FFF'}" shape="square" style="flex: none;"></u-search>
			</view>
		</view>

		<view class="content">
			<scroll-view class="scroll-view" @scrolltolower="scrolltolowerFn" scroll-y style="overflow: hidden;">
				<!-- 封装组件 -->
				<view class="card" v-for="(item,index) in dataList"
					style="border: 1px solid #ccc;border-radius: 20rpx;background-color: #fff;padding: 20rpx;margin: 20rpx;">
					<view class="title-content column">
						<view style="display: flex;padding: 20rpx 20rpx 0 20rpx; justify-content: space-between;">
							<view class="title" style="font-size: 32rpx;font-weight: bold;">
								优惠券名称:
							</view>
							<view class="title-c" style="padding: 10rpx;border-radius: 8rpx;">
								{{item.name}}
							</view>
						</view>
						<view style="border-bottom: 1px solid #f6f6f6;margin: 20rpx;"></view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								用户信息
							</view>
							<view class="title-c">
								{{item.nickname}}/{{item.uid}}
							</view>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								面值
							</view>
							<view class="title-c">
								{{item.money}}
							</view>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								最低消费额
							</view>
							<view class="title-c">
								{{item.minPrice}}
							</view>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								使用有效期
							</view>
							<view class="title-c" v-if="item.startTime" >
								{{item.startTime}} - {{item.endTime}}
							</view>
							<span v-else>-</span>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								适用范围
							</view>
							<view class="title-c">
								{{item.category | couponCategoryFilter}}
							</view>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								领取时间
							</view>
							<view class="title-c">
								{{ item.createTime }}
							</view>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								使用状态
							</view>
							<view class="title-c">
								<view v-if="item.status == 1">已使用</view>
								<span v-else-if="item.status == 0">未使用</span>
								<span v-else>已失效</span>
							</view>
						</view>
						<view style="display: flex;padding: 20rpx;justify-content: space-between;">
							<view class="title">
								使用时间
							</view>
							<view class="title-c">
								<span>{{item.useTime | filterEmpty}}</span>
							</view>
						</view>
					</view>
				</view>

				<view v-if="!dataList.length">
					<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
				</view>
				<!-- 上拉加载更多 -->
				<uni-load-more v-else :status="loadMoreStatus"></uni-load-more>

			</scroll-view>
		</view>



	</view>
</template>

<script>
	import {
		userCouponListApi
	} from '@/api/merchant.js';
	import {
		couponCategoryFilter,
		couponTypeFilter,
		couponUserTypeFilter,
		filterEmpty
	} from "../filter/filter.js"
	import {
		setThemeColor
	} from '../../../utils/setTheme';
	let app = getApp();
	export default {
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				loadMoreStatus: 'noMore', //加载更多
				keyWord: '',
				dataList: [],
				cancellForm: {
					page: 1,
					limit: 10,
				},
				totalPage: 1,
			};
		},
		onLoad() {
			this.bgColor = setThemeColor()
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
		},
		created() {
			this.initData();
		},
		// 过滤器
		filters: {
			couponCategoryFilter,
			couponTypeFilter,
			couponUserTypeFilter,
			filterEmpty
		},
		methods: {
			initData() {
				// 首次激活时被调用
				this.cancellForm.page = 1;
				this.loadMoreStatus = 'more';
				// //需要初始化列表数据，根据数据类型
				this.getCouponList();
			},
			// 触底事件 onReachBottom 加载更多数据
			scrolltolowerFn() {
				if (this.loadMoreStatus != 'more') {
					return;
				};
				this.cancellForm.page++;
				this.getCouponList(true);
			},
			onrefresh(e) {
				this.refreshing = true;
				this.refreshText = "刷新中...";
				setTimeout(() => {
					this.refreshing = false;
					this.refreshFlag = false;
					this.refreshText = "已刷新";
				}, 2000)
			},
			onpullingdown(e) {
				if (this.refreshing) {
					return;
				}
				this.pulling = false;
				if (Math.abs(e.pullingDistance) > Math.abs(e.viewHeight)) {
					this.refreshFlag = true;
					this.refreshText = "释放立即刷新";
				} else {
					this.refreshFlag = false;
					this.refreshText = "下拉可以刷新";
				}
			},
			// 搜索查询
			searchProduct(valueKey) {
				if (valueKey == '') {
					delete this.cancellForm.name;
					this.cancellForm.current = 1;
					this.loadMoreStatus = 'more';
					this.getCouponList();
				} else {
					this.cancellForm.current = 1;
					this.cancellForm.name = valueKey;
					this.loadMoreStatus = 'more';
					this.getCouponList();
				}
			},
			// // 开启状态筛选
			// statusTypeConfirm(status) {
			// 	this.cancellForm.current = 1;
			// 	this.cancellForm.status = status;
			// 	this.loadMoreStatus = 'more';
			// 	this.getCouponList()
			// },
			// ----------------------
			getCouponList(isMerge) {
				let that = this;
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				if (!isMerge) {
					this.cancellForm.page = 1;
					this.loadMoreStatus = 'more';
				};
				userCouponListApi(this.cancellForm).then((res) => {
					let data = res.data;
					if (isMerge) {
						that.$util.SplitArray(data.list, that.dataList);
					} else {
						that.dataList = data.list ? data.list : [];
					}
					that.totalPage = data.totalPage;
					if (data.totalPage == this.cancellForm.page) {
						that.loadMoreStatus = 'noMore';
					}
					if (data.totalPage == 0) {
						that.loadMoreStatus = 'noMore';
					}
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style lang="scss">
	.bg_color {
		@include main_bg_color(theme);
	}

	.plain_bgColor {
		@include main_color(theme);
		@include main_bg_rgba_color(theme, 0.05);
		border: 1rpx solid;
	}

	page {
		height: 100%;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;

		.header {
			z-index: 99;
			background-color: #fff;
		}

		.content {
			// min-height: 200px;
			overflow: hidden;
			flex: 1,
		}
	}

	.scroll-view {
		height: 100%;
	}



	.tab-view {
		display: flex;
		flex-direction: column;
		flex: 1;
	}



	// .content {
	// 	flex: 1;
	// 	overflow: hidden;
	// }
</style>